#ifndef GRAPHICS_H
#define GRAPHICS_H

#include "Engine_def.h"
#include <Windows.h>
#include <d3dx9.h>
#include "Math\MathTypes.h"
#include "Math\color.h"
#include "Math\Matrix44.h"

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define D3DFVF_CUSTOMVERTEX2 (D3DFVF_XYZ|D3DFVF_TEX1)

struct CUSTOMVERTEX

{

   float x, y, z;

   DWORD color;

   static unsigned int getFlags()

   {

       return D3DFVF_CUSTOMVERTEX;

   }

};

struct CUSTOMVERTEX2

{

   D3DXVECTOR3 pos;

   DWORD color;

   static unsigned int getFlags()

   {

       return D3DFVF_CUSTOMVERTEX;

   }

};

struct SRectangle2D

{

   Vect2f    m_vBottomLeft;

   Vect2f    m_vTopRight;

};

struct SCREEN_COLOR_VERTEX

{

   float x, y, z, w;

   DWORD color;

    float u, v;

   static unsigned int getFlags()

   {

       return(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1);

   }

};

typedef enum ETypeAlignment { CENTER, UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, LOWER_RIGHT };

class CCamera;
class CFrustum;

class CGraphicsManager
{
public:
	CGraphicsManager() :	m_pD3D(0), 
							m_bPaintSolid(1), 
							m_BackbufferColor_release(colBLACK),
							m_BackbufferColor_debug(colBLUE),
							m_Frustum(NULL){}
							/*m_yaw(0),
							m_pos(Vect3f(0.0,1.7f,0.0)),
							m_dir(v3fZERO)*/
	
	~CGraphicsManager();
	void				Init					(HWND hWnd, const SEngineParams& params);
	void				Deinit					();
	void				Update					();
	void				Render					();
	void				BeginRendering			();
	void				EndRendering			();
	void				SetupMatrices			(CCamera* camera);
	void				EnableAlphaBlend		();
	void				DisableAlphaBlend		();
	void				EnableZBuffering		();
	void				DisableZBuffering		();
	void				SetSolidPaintMode		(bool);
	void				ChangeSolidPaintMode	();
	void				DrawLine				( const Vect3f &PosA, const Vect3f &PosB, CColor Color);
	void				DrawAxis				(float size);
	void				DrawGrid				(float size, uint8 numlines, CColor color);
	void				DrawCube				(float size,CColor color);
	void				DrawSphere				(float radius,CColor color, uint8 vstacks, uint8 hstacks);
    void				SetTransform			(Mat44f& matrix);
	Vect2i				GetScreenRes			(){return Vect2i(m_uWidth,m_uHeight); }
	LPDIRECT3DDEVICE9	GetDevice				();
	void				DrawCamera				(CCamera* camera);

	bool				SphereVisible			(Vect3f& center, float radius);
	



	void        DrawRectangle2D    (    const Vect2i& pos, uint32 w, uint32 h, CColor& backGroundColor, uint32 edge_w, uint32 edge_h, CColor& edgeColor );

    //----QuadRender Functions----------------------------------------------

   void                                DrawQuad2D                                (const Vect2i& pos, uint32 w, uint32 h, ETypeAlignment alignment, CColor color = colBLUE);

   //void                                DrawQuad2D                                (const Vect2i& pos, uint32 w, uint32 h, ETypeAlignment alignment, CTexture* texture, SRectangle2D& coordText);

   //void                                DrawQuad2D                                (const Vect2i& pos, uint32 w, uint32 h, ETypeAlignment alignment, CTexture* texture, ETypeFlip flip = NONE_FLIP);



   

private:
	LPDIRECT3D9			m_pD3D;
	LPDIRECT3DDEVICE9	m_pD3DDevice;
	uint16				m_uWidth;
	uint16				m_uHeight;
	bool				m_bPaintSolid;
	CColor				m_BackbufferColor_debug;
	CColor				m_BackbufferColor_release;
	/*float				m_yaw;
	Vect3f				m_pos;
	Vect3f				m_dir;*/
	CFrustum*			m_Frustum;

private:
	void	GetWindowRect	(HWND hwnd);
	void    SetTransform    (D3DXMATRIX& matrix);
	void    CalculateAlignment                (uint32 w, uint32 h, ETypeAlignment alignment, Vect2i & posFinal);


};




#endif